import { Model, DataTypes, Optional } from 'sequelize';
import { sequelize } from '../../config/database';
import Room from './Room';
import User from './User';
import LiveSession from './LiveSession';
import FanClub from './FanClub';
import UserSnapshot from './UserSnapshot';

// 消息接口
interface MessageAttributes {
  id: string;
  roomId: string;
  userId?: string | null;
  sessionId?: string | null;
  userSnapshotId?: string | null;
  messageType: string;
  content?: string | null;
  createdAt?: Date;
}

// 创建时可选的属性
interface MessageCreationAttributes extends Optional<MessageAttributes, 'createdAt'> {}

// 消息模型类
class Message extends Model<MessageAttributes, MessageCreationAttributes> implements MessageAttributes {
  declare id: string;
  declare roomId: string;
  declare userId: string | null;
  declare sessionId: string | null;
  declare userSnapshotId: string | null;
  declare messageType: string;
  declare content: string | null;
  
  // 时间戳
  declare readonly createdAt: Date;
}

// 初始化模型
Message.init(
  {
    id: {
      type: DataTypes.STRING,
      primaryKey: true,
      allowNull: false
    },
    roomId: {
      type: DataTypes.STRING,
      allowNull: false
    },
    userId: {
      type: DataTypes.STRING,
      allowNull: true
    },
    sessionId: {
      type: DataTypes.STRING,
      allowNull: true
    },
    userSnapshotId: {
      type: DataTypes.UUID,
      allowNull: true
    },
    messageType: {
      type: DataTypes.STRING,
      allowNull: false
    },
    content: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  },
  {
    sequelize,
    tableName: 'messages',
    modelName: 'Message',
    timestamps: true,
    updatedAt: false, // 消息不需要更新时间
    indexes: [
      {
        fields: ['roomId', 'createdAt']
      },
      {
        fields: ['userId', 'createdAt']
      },
      {
        fields: ['sessionId', 'createdAt']
      },
      {
        fields: ['userSnapshotId']
      },
      {
        fields: ['messageType']
      },
      {
        fields: ['createdAt']
      }
    ]
  }
);

// 设置关联关系（禁用外键约束）
Message.belongsTo(Room, { foreignKey: 'roomId', as: 'room', constraints: false });
Message.belongsTo(User, { foreignKey: 'userId', as: 'user', constraints: false });
Message.belongsTo(LiveSession, { foreignKey: 'sessionId', as: 'session', constraints: false });
Message.belongsTo(UserSnapshot, { foreignKey: 'userSnapshotId', as: 'userSnapshot', constraints: false });
Message.belongsTo(FanClub, { 
  foreignKey: 'userId', 
  targetKey: 'userId', // 使用userId作为关联键，而不是主键
  as: 'fanClub', 
  constraints: false 
});

Room.hasMany(Message, { foreignKey: 'roomId', as: 'messages', constraints: false });
User.hasMany(Message, { foreignKey: 'userId', as: 'messages', constraints: false });
LiveSession.hasMany(Message, { foreignKey: 'sessionId', as: 'messages', constraints: false });
UserSnapshot.hasMany(Message, { foreignKey: 'userSnapshotId', as: 'messages', constraints: false });
FanClub.hasMany(Message, { 
  foreignKey: 'userId', 
  sourceKey: 'userId', // 使用userId作为源键，而不是主键
  as: 'messages', 
  constraints: false 
});

export default Message;